package cn.tworice.admin.web;

import cn.tworice.admin.service.LogService;
import cn.tworice.common.entity.AdminLog;
import cn.tworice.common.entity.LoginLog;
import cn.tworice.common.entity.dto.AdminLogDto;
import cn.tworice.common.vo.RequestResult;
import cn.tworice.common.vo.StatusCode;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 日志管理
 *
 * @Author 二饭
 * @Email 1786705138@qq.com
 * @Date 2021/12/4 19:49
 */
@RestController
@RequestMapping("admin/log")
@Slf4j
@CrossOrigin
public class LogController {
    @Autowired
    private LogService logService;

    /**
     * 获取操作日志
     * @param pageSize 每页数量
     * @param page 页码
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/5 21:46
     */
    @GetMapping("adminList")
    @ApiOperation("操作日志")
    public RequestResult adminList(Integer pageSize,Integer page) {
        RequestResult result = new RequestResult();
        List<AdminLogDto> list = logService.getAdminList(pageSize,page);
        result.setStatus(new StatusCode(200, "获取管理员操作日志"));
        result.appendData("logList", list);
        result.appendData("total",logService.getAdminLogTotal());
        return result;
    }

    @DeleteMapping("delAdmin")
    @ApiOperation("删除管理员操作日志")
    public RequestResult delAdmin(String[] id,Integer pageSize,Integer page){
        RequestResult result = new RequestResult();
        int i=logService.delAdminLog(id);
        result.setStatus(new StatusCode(200,"删除"+i+"条操作日志"));
        result.appendData("logList",logService.getAdminList(pageSize,page));
        result.appendData("total",logService.getAdminLogTotal());
        return result;
    }

    @GetMapping("loginList")
    @ApiOperation("登录日志")
    public RequestResult loginList(Integer pageSize,Integer page){
        RequestResult result = new RequestResult();
        List<LoginLog> list = logService.getLoginList(pageSize,page);
        result.setStatus(new StatusCode(200, "获取管理员操作日志"));
        result.appendData("logList", list);
        result.appendData("total",logService.getLoginLogTotal());
        return result;
    }
}
